package com.weixin.paleituo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.annotation.RankAreaType;
import com.weixin.paleituo.bean.AreaInfoBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 购物地址区域选择Fragment
 *
 * @author fengzhen
 * @version v1.0, 2018/3/29
 */
public class ShippingAreaFragment extends Fragment {

    private static final String ARG_AREA = "arg_area";
    private static final String ARG_RANK = "arg_rank";
    private static final String ARG_CURRENT = "arg_current";
    private AreaListAdapter mAreaListAdapter;

    public interface OnAreaChooseListner {
        /**
         * 地区被选择
         *
         * @param rank     当前等级
         * @param area     区域名
         * @param nextArea 下一个区域的所有数据
         * @author fengzhen
         * @version v1.0, 2018/3/30
         */
        void onAreaChoose(@RankAreaType int rank, AreaInfoBean area, List<AreaInfoBean> nextArea);
    }

    private ArrayList<AreaInfoBean> mArea = new ArrayList<>();
    @RankAreaType
    private int mRank = RankAreaType.AREA_DEFAULT;
    private ListView mAreaListView;

    private OnAreaChooseListner mAreaChooseListenre;
    // 当前选中位置
    private int mCurrentChoice = -1;
    private AreaInfoBean mCurrentArea;

    /**
     * 创建区域fragment
     *
     * @param area        区域数据
     * @param rank        省 - 市 - 区/县 等级
     * @param currentArea 选中的地区
     * @author fengzhen
     * @version v1.0, 2018/3/30
     */
    public static ShippingAreaFragment newInstance(ArrayList<AreaInfoBean> area, @RankAreaType int rank, AreaInfoBean currentArea) {
        ShippingAreaFragment fragment = new ShippingAreaFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_AREA, area);
        args.putInt(ARG_RANK, rank);
        args.putParcelable(ARG_CURRENT, currentArea);
        fragment.setArguments(args);
        return fragment;
    }

    public void setArea(ArrayList<AreaInfoBean> area) {
        this.mArea.clear();
        this.mArea.addAll(area);
        mAreaListAdapter.notifyDataSetChanged();
        mCurrentChoice = -1;
    }

    /**
     * 监听区域被选择事件
     *
     * @author fengzhen
     * @version v1.0, 2018/3/30
     */
    public void setOnAreaChooseListenre(OnAreaChooseListner mAreaChooseListenre) {
        this.mAreaChooseListenre = mAreaChooseListenre;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        mRank = arguments.getInt(ARG_RANK);
        Log.i("info", "onCreate: ====+++" + arguments.getSerializable(ARG_AREA));
        if (arguments.getSerializable(ARG_AREA) == null) return;
        mArea.addAll((ArrayList<AreaInfoBean>) arguments.getSerializable(ARG_AREA));
        // 获取默认选择项
        mCurrentArea = arguments.getParcelable(ARG_CURRENT);
        if (mCurrentArea != null) {
            for (int i = 0, j = mArea.size(); i < j; i++) {
                if (mArea.get(i).getId() == mCurrentArea.getId()) {
                    mCurrentChoice = i;
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mAreaListView = (ListView) inflater.inflate(R.layout.fragment_area_list, container, false);

        mAreaListAdapter = new AreaListAdapter();
        mAreaListView.setAdapter(mAreaListAdapter);
        mAreaListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurrentChoice = position;
                mAreaListAdapter.notifyDataSetChanged();
                // 传递数据以及位置 外抛到Dialog进行处理
                if (mAreaChooseListenre != null) {
                    mAreaChooseListenre.onAreaChoose(
                            mRank, mArea.get(position), mArea.get(position).getChildren());
                }
            }
        });

        // 展示选中项 并 设置选中效果
        if (mCurrentChoice != -1) {
            mAreaListView.setItemChecked(mCurrentChoice, true);
            mAreaListView.setSelection(mCurrentChoice);
        }

        return mAreaListView;
    }

    /**
     * 区域选择adapter
     *
     * @author fengzhen
     * @version v1.0, 2018/3/30
     */
    private class AreaListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mArea.size();
        }

        @Override
        public Object getItem(int position) {
            return mArea.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_area_name, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            // 设置选中效果
            if (position == mCurrentChoice) {
                viewHolder.mTvAreaItemArea.setEnabled(false);
            } else {
                viewHolder.mTvAreaItemArea.setEnabled(true);
            }

            viewHolder.mTvAreaItemArea.setText(mArea.get(position).getName());

            return convertView;
        }

    }

    /**
     * 区域列表ViewHolder
     *
     * @author fengzhen
     * @version v1.0, 2018/3/30
     */
    static class ViewHolder {
        @BindView(R.id.tv_area_item_area)
        TextView mTvAreaItemArea;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
